package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> lists() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        //通过id查询区域的refids
       Region region=regionService.findById(rid);
        //通过refids查询他对应的目的的
        List<String> refId = region.getRefIds();
        //通过apl命名来查询
        return destinationRepository.findByIdIn(refId);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        //创建查询对象
        Query query=new Query();
        //添加条件所有字符串都要判空的
        if (StringUtils.hasLength(qo.getKeyword())){
            Criteria regex = Criteria.where("name").regex(qo.getKeyword());
            query.addCriteria(regex);
        }
        if (StringUtils.hasLength(qo.getParentId())){
            Criteria regex = Criteria.where("parentId").is(qo.getParentId());
            query.addCriteria(regex);
        }
     /*   else {
            Criteria regex = Criteria.where("parentId").is(null);
            query.addCriteria(regex);
        }*/
        //查询总数
        long count = mongoTemplate.count(query, Destination.class);
        //分页查询
        PageRequest pageRequest=PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageRequest);
        //查询数据
        List<Destination> list = mongoTemplate.find(query, Destination.class);
        return new PageImpl<>(list,pageRequest,count);
    }
    //利用递归方法实现
    @Override
    public List<Destination> createTost(String parentId) {
        if (!StringUtils.hasLength(parentId)){
            return Collections.emptyList();
        }
        //创建一个空的集合放父类
        List<Destination> lists=new ArrayList<>();
       this.get2createTost(lists,parentId);
       //集合翻转
        Collections.reverse(lists);
        return lists;
    }
    //这个是一个递归方法
    private void get2createTost( List<Destination> lists,String parentId) {
        //判断,如果父类id为空,就是顶级了
        if(!StringUtils.hasLength(parentId)){
            return;
        }
        //如果有长度就通过父类id拿到自己对象
        Destination destination = this.get(parentId);
        //把对象放入数组中
        lists.add(destination);
        //判断拿到的对象也没有父类id,有就拿直接id
        if (StringUtils.hasLength(destination.getParentId())){
            get2createTost(lists,destination.getParentId());
        }
    }
    //通过目的地id查询
    @Override
    public List<Destination> queryDestByRegionId(String regionId) {
        //有多个地方用到集合
        List<Destination> list= new ArrayList<>();
        if (regionId.equals("-1")){
            //通过名字查询,国内
            list=destinationRepository.findByParentName("中国");
        }else {
            //国外,t通过父id查询子的,通过字的查询集合
             list = this.getDestByRegionId(regionId);
        }
        //直接在这里查询第二层,通过id查询目的地
        for(Destination ds :list){
            //用分页截取
            PageRequest pageRequest= PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            List<Destination> dest =destinationRepository.findByParentId(ds.getId(),pageRequest);
            ds.setChildren(dest);
        }
        return list;
    }

    @Override
    public Destination queryfindByName(String keyword) {
        return destinationRepository.findByName(keyword);
    }

}
